package cn.com.dev.temp;

import cn.com.kh.KHUtil;
import cn.com.kh.TxConfig;
import cn.com.kh.batch.FCallAPI_Utils;
import cn.com.kh.result.iAPIResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @projectName: ngsapitask-ph
 * @package: cn.com.dev
 * @className: FDD_SDK
 * @author: LiJT
 * @description: TODO
 * @date: 2024-06-27 18:25
 * @version: 1.0
 */
public class FDD_FixSignTask {
    private static final Log log = LogFactory.getLog(FDD_FixSignTask.class);
    private static String CC_APIDomain = "https://ronglian.dbc61.com/ngsapitask_ph/api";
    private static String APPID = "100002";
    private static String APPKEY = "E4uX9c65e5h7A5K5DA6rw6JL";

    // 获取数据库操作对象
    private static JdbcTemplate jt = new JdbcTemplate();

    public static void main(String[] args) throws Exception {
        jt = new JdbcTemplate(TxConfig.getDruidDataSource());

        // 获取待认证客户状态
        batchFDDSignFix("SZHJX-ZL-A3004-20230701PHkO");

    }

    // Fix-1-获取合同签署状态
    public static void batchFDDSignFix(String doc_No) {
        do_GetSignStateInfo(doc_No);
    }

    // Fix-2-获取合同签署状态
    public static JSONObject do_GetSignStateInfo(String doc_No) {

        JSONObject res_Json = iAPIResult.ERROR("获取合同签署状态 失败");
        try {
            // 查询-FDD-通过合同ID查询法大大合同签署状态
            String Rec_Guid = KHUtil.getRandomString(36).toUpperCase();
            res_Json = FCallAPI_Utils.getContractSignStatus(jt, Rec_Guid, doc_No);
            log.debug("☢通过合同ID查询法大大合同签署状态 :" + res_Json);

            String res_Code = KHUtil.GetSFJV(res_Json, "code");
            String res_Msg = KHUtil.GetSFJV(res_Json, "msg");
            if (res_Code.equals("1")) {
                JSONObject res_FddJson = res_Json.getJSONObject("data");
                res_Code = KHUtil.GetSFJV(res_FddJson, "code");
                res_Msg = KHUtil.GetSFJV(res_FddJson, "msg");
                if (res_Code.equals("1")) {
                    JSONObject res_FddData = res_FddJson.getJSONObject("data");
                    JSONArray res_signerList = res_FddData.getJSONArray("contractSignerList");

                    for (int i = 0; i < res_signerList.size(); i++) {
                        JSONObject res_FddSign = res_signerList.getJSONObject(i);

                        String customerId = KHUtil.GetSFJV(res_FddSign, "customerId");
                        String signStatus = KHUtil.GetSFJV(res_FddSign, "signStatus");
                        String signStatusDesc = KHUtil.GetSFJV(res_FddSign, "signStatusDesc");
                        String signTime = KHUtil.GetSFJV(res_FddSign, "signTime");
                        String reject_Reason = signStatus.equals("2") ? "已拒签,签署状态接口无详细原因" : "";

                        updateCont_SignState(doc_No, customerId, signStatus, signStatusDesc, signTime, reject_Reason);

                    }

                } else {

                    log.warn("⛔法大大签署状态接口失败 结果:" + res_Code + "/" + res_Msg);

                }
            } else {

                log.warn("⛔内部签署状态接口失败 结果:" + res_Code + "/" + res_Msg);

            }

        } catch (Exception e) {
            log.error("☣内部签署状态接口 异常", e);

        }
        return res_Json;

    }


    /* 合同签署状态数据修复 SZHJX-ZL-AC12-202311015XHp*/
    public static void updateCont_SignState(String doc_No, String customerId, String signStatus, String signStatusDesc, String signTime, String reject_Reason) {
        String updSql = "Insert Into Fdd_Agreement_List_His (Agreementduid, Modelid, Businessnumber, Workorderid, Contractid, Pushsigning, Customerid, Ht_State, Ht_Type, Pushsigningtime, Ht_Statetime, Ordernum, Transactionno, Customertype, Rejectionreason, Createddate, Createdby, Resultcode, Resultmsg, Resulttime, Pushfailreason, Pushfailtime, Del_Flag, Update_By, Update_Time) Select Agreementduid, Modelid, Businessnumber, Workorderid, Contractid, Pushsigning, Customerid, Ht_State, Ht_Type, Pushsigningtime, Ht_Statetime, Ordernum, Transactionno, Customertype, Rejectionreason, Createddate, Createdby, Resultcode, Resultmsg, Resulttime, Pushfailreason, Pushfailtime, 1, '##PULL', Now() From Fdd_Agreement_List Where Contractid = ? And Customerid = ?";
        int i_Num = jt.update(updSql, doc_No, customerId);
        log.debug("❕合同签署状态数据修复 备份数据 i_Num:" + i_Num);

        updSql = "Update Fdd_Agreement_List Set Ht_State = ?, Ht_Statetime = ?, Rejectionreason = ?, Resultcode = ?, Resultmsg = ?, Resulttime = Now() Where Contractid = ? And Customerid = ?";
        i_Num = jt.update(updSql, signStatusDesc.equals("已签") ? "已签署" : signStatusDesc, KHUtil.isNull(signTime) ? null : signTime, reject_Reason, signStatus, signStatusDesc, doc_No, customerId);
        log.debug("❕合同签署状态数据修复 更新数据 i_Num:" + i_Num);
    }
}